package com.example.zzl.view.fragment.SearchFragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzl.R;
import com.example.zzl.adapter.SearchFragment.CifGvAdapter;
import com.example.zzl.databinding.ClassifyfragmentLayoutBinding;
import com.example.zzl.model.bean.SearchFragment.Cat;
import com.example.zzl.model.cans.Cans;
import com.example.zzl.utils.ListViewUtils;
import com.example.zzl.view.activity.MainActivity;
import com.example.zzl.view.activity.SearchActivity.CategoryActivity;
import com.example.zzl.view.activity.SearchActivity.SearchResultActivity;
import com.example.zzl.viewmodel.SearchFragment.Classify;
import com.example.zzl.viewmodel.SearchFragment.ResultData;

import java.util.ArrayList;

/**
 * Created by zhang on 2017/2/8.
 */

public class ClassifyFragment extends Fragment implements AdapterView.OnItemClickListener{

    private ClassifyfragmentLayoutBinding binding;
    private GridView gv_1,gv_2;
    private TextView tv_1,tv_2;
    private ArrayList<Object> list1,list2;
    private CifGvAdapter adapter1,adapter2;
    private Classify classify;
    private ProgressBar progressBar;
    private ScrollView sc;
    public static ClassifyFragment getInstance() {
        Bundle args = new Bundle();
        ClassifyFragment fragment = new ClassifyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list1 = new ArrayList<>();
        list2 = new ArrayList<>();
        adapter1 = new CifGvAdapter(getActivity(),list1);
        adapter2 = new CifGvAdapter(getActivity(),list2);
        classify = new Classify(Cans.CLASSIFY_URl,"Cif");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.classifyfragment_layout,container,false);
        progressBar = binding.pbLoad;
        sc = binding.scCif;
        sc.smoothScrollTo(0,0);
        gv_1 = binding.gvCif1;
        gv_2 = binding.gvCif2;
        gv_1.setAdapter(adapter1);
        gv_2.setAdapter(adapter2);
        tv_1 = binding.tvCla1;
        tv_2 = binding.tvCla2;
        gv_1.setEmptyView(progressBar);
        gv_1.setOnItemClickListener(this);
        gv_2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent1 = new Intent(getActivity(),SearchResultActivity.class);
                intent1.putExtra("search",MainActivity.resultData.malls.get(i).getStrTagName());
                intent1.putExtra("mallUrl",MainActivity.resultData.malls.get(i).getStrTagUrl());
                intent1.putExtra("type","Mall");
                startActivity(intent1);
            }
        });
        loadData();
        return binding.getRoot();
    }

    private void loadData(){
        MainActivity.handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 0x101){
                    tv_1.setText(MainActivity.resultData.cat.getStrGroupName());
                    for (int i = 0; i < MainActivity.resultData.cat.getListHotTag().size(); i++) {
                        list1.add(MainActivity.resultData.cat.getListHotTag().get(i));
                    }
                    adapter1.notifyDataSetChanged();
                    ListViewUtils.calGridViewWidthAndHeigh(3,gv_1);
                }else if(msg.what == 0x102){
                    tv_2.setText(MainActivity.resultData.mall.getStrGroupName());
                    for (int i = 0; i < MainActivity.resultData.mall.getListHotTag().size(); i++) {
                        list2.add(MainActivity.resultData.mall.getListHotTag().get(i));
                        //Log.e("data",MainActivity.resultData.mall.getListHotTag().get(i).getStrTagName());
                    }

                    adapter2.notifyDataSetChanged();
                    ListViewUtils.calGridViewWidthAndHeigh1(4,gv_2);
                }
            }
        };



    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //Toast.makeText(getActivity(), "33333", Toast.LENGTH_SHORT).show();
//        switch (view.getId()) {
//            case R.id.gv_cif_1:
                Intent intent = new Intent(getActivity(), CategoryActivity.class);
                intent.putExtra("url", Cans.CATE_URL + MainActivity.resultData.cats.get(i).getStrTagUrl());
                intent.putExtra("title", MainActivity.resultData.cats.get(i).getStrTagName());
                startActivityForResult(intent, 400);
//                break;
//            case R.id.gv_cif_2:

               // break;
        //}
    }
}
